package com.gudaomai.ekproject.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gudaomai.ekproject.common.CommonConstant;
import com.gudaomai.ekproject.common.CommonException;
import com.gudaomai.ekproject.common.MsgConstant;
import com.gudaomai.ekproject.entity.ProjectSysuser;
import com.gudaomai.ekproject.mapper.ProjectSysuserMapper;
import com.gudaomai.ekproject.service.IProjectSysuserService;
import com.gudaomai.ekproject.service.ISysuserService;
import com.gudaomai.ekproject.service.impl.BaseServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * project_sysuser 服务实现类
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@Service
public class ProjectSysuserServiceImpl extends BaseServiceImpl<ProjectSysuserMapper, ProjectSysuser>
		implements IProjectSysuserService {

	@Autowired
	protected MessageSource messageSource;
	@Autowired
	ProjectSysuserMapper projectSysuserMapper;
	@Autowired
	ISysuserService sysuserServ;

	@Override
	public ProjectSysuser selectProjectSysuserBySysuserId(String uid) {
		return null;
	}

	// 通过用户id获取所有对应的project_id[多对多的关系]
	@Override
	public List<ProjectSysuser> getProjectByuserId(String uid) throws CommonException {

		EntityWrapper<ProjectSysuser> ew=new EntityWrapper<>();
		ew.setEntity(new ProjectSysuser());
		ew.eq("sysuser_id", uid);
		
		List<ProjectSysuser> projectSysusers=this.selectList(ew);
		
		if (null != sysuserServ.selectById(uid) && projectSysusers.size() > 0) {
			// 查询到数据，返回查询结果集合
			return projectSysusers;
		} else {
			// 没有查询到数据，抛出异常
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_FOUND,
					getMessage(MsgConstant.RET_PROJECTSYSUSER_SELECT_NOTEXIST));
		}

	}

	// 根据pid,uid删除某条记录
	@Override
	public boolean delProjectUser(String projectId, String uid) throws CommonException {

		EntityWrapper<ProjectSysuser> ew=new EntityWrapper<>();
		ew.setEntity(new ProjectSysuser());
		ew.eq("project_id", projectId).eq("sysuser_id", uid);
		return this.delete(ew);

	}

	// 检查某条记录是否存在
	@Override
	public boolean isExist(String projectId, String uid) throws CommonException {

		EntityWrapper<ProjectSysuser> ew=new EntityWrapper<>();
		ew.setEntity(new ProjectSysuser());
		ew.eq("project_id", projectId).eq("sysuser_id", uid);
		
		ProjectSysuser projectSysuser=this.selectOne(ew);
		
		// 数据库不存在，返回false
		if (projectSysuser == null) {
			return false;
		}
		return true;
	}

	
}
